package vip.yyzhu.redisadmin.api.pub.controller;

import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import vip.yyzhu.redisadmin.aop.permissions.AuthUnique;
import vip.yyzhu.redisadmin.aop.verifyKeyType.VerifyKeyType;
import vip.yyzhu.redisadmin.jopo.ResData;
import vip.yyzhu.redisadmin.jopo.ResJson;
import vip.yyzhu.redisadmin.redis.RedisData;
import vip.yyzhu.redisadmin.redis.RedisList;
import vip.yyzhu.redisadmin.util.Tool;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping(value = "/api/db", produces = "application/json;charset=UTF-8")
@SuppressWarnings("all")
public class DbController {
    @Autowired
    RedisList redisList;
    @Autowired
    Tool tool;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object getInfo(HttpServletRequest request,
                          @RequestParam(required = false, defaultValue = "127.0.0.1") String host,
                          @RequestParam(required = false, defaultValue = "6379") int port,
                          @RequestParam(required = false, defaultValue = "") String pass) {
        String unique = Tool.getUnique(request);
        System.out.println(host);
        System.out.println(port);
        System.out.println(pass);
        try {
            //连接本地的 Redis 服务
            Jedis jedis = new Jedis(host, port);
            // 如果 Redis 服务设置了密码，需要下面这行，没有就不需要

            if (pass != null && !"".equals(pass)) {
                jedis.auth(pass);
            }
            System.out.println("连接成功");
            //查看服务是否运行
            System.out.println("服务正在运行: " + jedis.ping());
            RedisData data = new RedisData();
            data.setHost(host);
            data.setPort(port);
            data.setPass(pass);
            data.setJedis(jedis);
            redisList.add(unique, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResJson(4044, "连接失败");
        }
        return new ResJson(200, "成功");
    }

    @AuthUnique
    @RequestMapping(value = "/ping", method = RequestMethod.GET)
    public Object ping(HttpServletRequest request) {
        val data = tool.getJedisData(request);
        Map map = new HashMap();
        map.put("host",data.getHost());
        map.put("port",data.getPort());
        map.put("pass",data.getPass());
        return new ResJson(200, "成功", new ResData(map));
    }
    @AuthUnique
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Object info(HttpServletRequest request) {
        String unique = Tool.getUnique(request);
        val jedis = redisList.getJedis(unique);
        String info = jedis.info();
        return new ResJson(200, "成功", info);
    }

}
